Application example of GPIO function

Latest update: April 2018

Overview

This tutorial introduces the GPIO function of FlashAir IoT Hub.

With the GPIO function, you can flicker an LED from FlashAir IoT Hub through FlashAir's GPIO and get the value of the switch.

How to operate the screen

GPIO input

GPIO input

The value of each pin of GPIO input set to "Trigger > GPIO input" on the screen is displayed as High or Low. The output set pin is displayed as (Output).

However, with the initial setting of FlashAir, the GPIO function is disabled, and the value is not displayed. When using GPIO please enable GPIO function referring to CONFIG setting.

GPIO output

GPIO output

You can set the value to be output to each pin of GPIO with High or Low by the switch of "Action > GPIO output" on the screen.

After setting the value to be output with the switch, click the execution button to send the instruction to set the GPIO value to FlashAir.

The instruction to set the GPIO is registered as a job, and when FlashAir executes the job status changes from "Unexecuted" to "Executed".

Job

LED flicker

Using the GPIO function of FlashAir IoT Hub, let's try flicking an LED (operate the LED blinking).

To use FlashAir's GPIO feature, this tutorial uses FlashAir prototyping board Airio RP.

For Airio RP, please refer to FlashAir prototyping board "Airio RP".

Constitution

Using Airio RP and breadboard, connect as follows.

Picture of actual machine
Circuit diagram

The Airio RP lights up when High is input to the LED terminal, and can be turned off by inputting Low, so connect D0 (0x02) to the LED terminal and control flicking at the output of D0.

The circuit diagram is created by Frizing in the schematic drawing software. By using Frizing's library published on Developer Resources, you can easily create circuit diagrams such as FlashAir and Airio RP, Please use it.

I/O setting

Set the input / output according to the Airio RP pin assignment. Edit the sample script bootscript.lua downloaded from FlashAir IoT Hub as follows and save it in the root directory of FlashAir.

In this tutorial, only the processing related to GPIO is explained. Other processing will be explained in other tutorials.

bootscript.lua

local iothub = require("iothub")

iothub.startPioUpload(0x01)
-- iothub.stopPioUpload()

local cnt = 0
while(1) do
  iothub.runJob()
  iothub.addMeasurement({10, 20, cnt})
  cnt = cnt + 1
  sleep(10000)
  collectgarbage("collect")
end
  • Line 3:
    Remove the comment (--) and set the GPIO I/O settings in iothub.startPioUpload(ctrl).
    Please set the value of hexadecimal number specifying 1 for the bit corresponding to the pin you want to read and 0 for the bit corresponding to the pin you want to write.
    In this tutorial, change the CMD (0x01) of FlashAir for reading and the others for writing, so change the value to 0x01.
  • Line 4:
    Although not used in this tutorial, if you do not upload the GPIO value to FlashAir IoT Hub, please uncomment and execute iothub.stopPioUpload().
  • Line 8:
    Communicating with FlashAir IoT Hub by running iothub.runJob(), upload values of input GPIOs and download GPIO output instructions (jobs) issued from FlashAir IoT Hub.

CONFIG setting

Edit /SD_WLAN/CONFIG in FlashAir and add IFMODE=1 in order to enable the GPIO function of FlashAir.

Launch FlashAir

Pierce FlashAir into Airio RP, insert Micro USB cable connected to power supply and start up.

After start up, confirm that the LED of the Airio RP turns off, if it does, it's ready.


Execution result

Now that we are ready, let's run it.

GPIO output

Set D0 (0x02) to High at the GPIO output of FlashAir IoT Hub and click the execute button.

LED flicker

You can confirm that the LED of the Airio RP lights up.

In this tutorial I did LED flicker using the LED of the Airio RP, but if you connect FlashAir you can remotely control your favorite device from the FlashAir IoT Hub. Please try whatever application you wish.